package com.bytedance.video.common;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import java.io.InputStream;

public class QiniuUtil {

    private static String accessKey = "bl4atokRnVUAr4VAi205a7CfAszKv2S0QM5ZOjhQ";
    private static String secretKey = "KE4wxQMh22WNfUVqzTRQJux36f0d4hifUTZQBF2d";
    private static String bucket = "tiktok-xiaoyu";

    //构造一个带指定 Region 对象的配置类
    private static Configuration cfg = new Configuration(Region.huanan());

    private static Auth auth = Auth.create(accessKey, secretKey);


    public static String UploadByFile(String fileName, String localFilePath) {
        UploadManager uploadManager = new UploadManager(cfg);
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        String http = null;
        try {
            Response response = uploadManager.put(localFilePath, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            http = "http://rynthab3j.hn-bkt.clouddn.com/" + putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return http;
    }

    public static String UploadByInput(String fileName, InputStream inputStream) {
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        //        String key = null;
        String http = null;
        try {

            String upToken = auth.uploadToken(bucket);
            try {
                UploadManager uploadManager = new UploadManager(cfg);
                cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
                Response response = uploadManager.put(inputStream, fileName, upToken, null, null);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                //文件名称
                System.out.println(putRet.key);
                //拼接路径
                http = "http://rynthab3j.hn-bkt.clouddn.com/" + putRet.key;
                System.out.println(putRet.hash);
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (Exception ex) {
            //ignore
        }
        return http;
    }

    public static String deleteFile(String fileName) {
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, fileName);
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
            throw new RuntimeException(ex);
        }
        return "删除成功！";
    }


}
